"""
Case Type   : GUC
Case Name   : 使用gs_guc set方法设置参数vacuum_freeze_table_age为无效值，
              合理报错
Create At   : 2020/12/2
Owner       : zhanghuan96
Description :
    1.查询vacuum_freeze_table_age默认值
    2.修改参数值为test
    3.修改参数值为158553.4154545
    4.修改参数值为-1
    5.修改参数为空字符串
    6.恢复参数默认值
Expect      :
    1.显示默认值为4000000000
    2.合理报错
    3.合理报错
    4.合理报错
    5.合理报错
    6.默认值恢复成功
History     :
"""
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

LOG = Logger()
commonsh = CommonSH('dbuser')


class ClientConnection(unittest.TestCase):
    def setUp(self):
        LOG.info(
            '----Opengauss_Function_Guc_ClientConnection_Case0094start----')
        self.constant = Constant()

    def test_vacuum_freeze_table_age(self):
        LOG.info('--步骤1:查看默认值--')
        sql_cmd = commonsh.execut_db_sql('''show vacuum_freeze_table_age;''')
        LOG.info(sql_cmd)
        self.res = sql_cmd.splitlines()[-2].strip()
        LOG.info('--步骤2:依次修改参数值为test,158553.4154545,-1,"''"--')
        invalid_value = ['test', 158553.4154545, -1, "''"]
        for i in invalid_value:
            result = commonsh.execute_gsguc("set",
                                            self.constant.GSGUC_SUCCESS_MSG,
                                            f"vacuum_freeze_table_age={i}")
            self.assertFalse(result)

    def tearDown(self):
        LOG.info('--步骤3:恢复默认值--')
        sql_cmd = commonsh.execut_db_sql('''show vacuum_freeze_table_age;''')
        LOG.info(sql_cmd)
        if sql_cmd.split('\n')[-2].strip() != self.res:
            msg = commonsh.execute_gsguc('set',
                                         self.constant.GSGUC_SUCCESS_MSG,
                                         f'vacuum_freeze_table_age={self.res}')
            LOG.info(msg)
            msg = commonsh.restart_db_cluster()
            LOG.info(msg)
        status = commonsh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)
        sql_cmd = commonsh.execut_db_sql('''show vacuum_freeze_table_age;''')
        LOG.info(sql_cmd)
        LOG.info(
            '---Opengauss_Function_Guc_ClientConnection_Case0094执行完成--')
